<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active{
            background-color: pink;
            font-size:30px;
            color:red;
        }
    </style>
</head>

<body>
    <div id="app">       
        <span :class="{active:index==0}" @click="changeCom(0)">娱乐</span>|
        <span :class="{active:index==1}" @click="changeCom(1)">八卦</span>|
        <span :class="{active:index==2}"  @click="changeCom(2)">体育</span>
        <div>
            <keep-alive>
                <component :is="currCom"></component>
            </keep-alive>
        </div>
    </div>

    <script src="./js/vue.js"></script>
    <script>
        let yuLe = {
            template: "<div>娱乐新闻</div>",
            beforeCreate(){
                console.log("====yuLe:beforeCreate=====");
            },
            beforeDestroy(){
                console.log("====yuLe:beforeDestroy=====");
            },
            destroyed(){
                console.log("====yuLe:destroyed=====");
            },
            activated(){
                console.log("====yuLe:activated=====");
            },
            deactivated(){
                console.log("====yuLe:deactivated=====");
            }
        }
        let eightGua = {
            template: "<div>八卦新闻</div>",
            beforeCreate(){
                console.log("====eightGua:beforeCreate=====");
            },
            beforeDestroy(){
                console.log("====eightGua:beforeDestroy=====");
            },
            destroyed(){
                console.log("====eightGua:destroyed=====");
            },
            activated(){
                console.log("====eightGua:activated=====");
            },
            deactivated(){
                console.log("====eightGua:deactivated=====");
            }
        }
        let sports = {
            template: "<div>体育新闻</div>"
        }


        new Vue({
            el: "#app",
            data: {
                currCom:yuLe,
                index:0,
                coms:[yuLe,eightGua,sports]
            },
            methods:{
                changeCom(idx){
                    this.index = idx;
                    this.currCom = this.coms[idx];
                }
            }
        });

    </script>

</body>

</html>